from PyQt5.QtCore import QJsonDocument, QByteArray

input_files = [
    './actionstest/取杯子',
    './actionstest/放中45.txt',
    './actionstest/等待',
    './actionstest/拿中45',
    './actionstest/放到盖盖子处',
    './actionstest/放盖子.txt',
    './actionstest/下降.txt',
    './actionstest/等待',
    './actionstest/上升.txt',
]
# file_name = './actionstest/拿中.txt'
output_file_name = './actionstest/mid_action.txt'
param = "Get_Right_Cmds"

# with open(file=output_file_name, mode='w+', encoding='utf-8') as output_file:
#     output_file.write("String {}[] =\n".format(param))
#     output_file.write("{\n")
#     output_file.write("  \"{}\"{}".format("G94", ",\n"))
#
#     for file_name in input_files:
#         print(file_name)
#         output_file.write("  // file name : {}\n".format(file_name))
#         with open(file=file_name, mode='r', encoding='utf-8') as file:
#             # print(file)
#             json_array_str = file.read()
#             # print(json_array_str)
#             json_obj = QJsonDocument.fromJson(QByteArray(str(json_array_str).encode("utf-8"))).object()
#             # print(json_obj)
#             array_obj = json_obj["array"]
#             array = array_obj.toArray()
#
#             # for j in array:
#             #     print(j.toObject()["cmd"].toString())
#
#             for j in array:
#                 cmd = j.toObject()["cmd"].toString().replace("\r", "", 1)
#                 output_file.write("  \"{}\"{}".format(cmd, ",\n"))
#             output_file.write("\n")
#
#     output_file.write("};\n")


with open(file=output_file_name, mode='w+', encoding='utf-8') as output_file:
    # output_file.write("String {}[] =\n".format(param))
    # output_file.write("{\n")
    # output_file.write("  \"{}\"{}".format("G94", ",\n"))

    data = QByteArray()
    json = QJsonDocument.fromJson(data).object()  # 创建空的QJsonObject对象
    jsonarry = QJsonDocument.fromJson(data).array()  # 创建空的QJsonArray对象。
    # for i in range(count):
    #     item = self.ui.listWidget.item(i)
    #     s = item.text()
    #
    #     json_pos = QJsonDocument.fromJson(data).object()
    #     json_pos["cmd"] = s
    #     jsonarry.append(json_pos)

    output_json = QJsonDocument(json).toJson(QJsonDocument.Indented)
    # with open(file=path, mode='w+', encoding='utf-8') as file:
    #     # file.write(text)
    #     file.write(output_json.data().decode("utf-8"))

    for file_name in input_files:
        print(file_name)
        # output_file.write("  // file name : {}\n".format(file_name))
        with open(file=file_name, mode='r', encoding='utf-8') as file:
            # print(file)
            json_array_str = file.read()
            # print(json_array_str)
            json_obj = QJsonDocument.fromJson(QByteArray(str(json_array_str).encode("utf-8"))).object()
            # print(json_obj)
            array_obj = json_obj["array"]
            array = array_obj.toArray()

            # for j in array:
            #     print(j.toObject()["cmd"].toString())

            for j in array:
                # cmd = j.toObject()["cmd"].toString().replace("\r", "", 1)
                # output_file.write("  \"{}\"{}".format(cmd, ",\n"))
                # for i in range(count):

                item = j.toObject()["cmd"].toString()
                print(item)
                s = item

                json_pos = QJsonDocument.fromJson(data).object()
                json_pos["cmd"] = s
                jsonarry.append(json_pos)

    json["array"] = jsonarry
    output_json = QJsonDocument(json).toJson(QJsonDocument.Indented)
    output_file.write(output_json.data().decode("utf-8"))
